<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        添加
      </el-button>
    </div>
    <div class="wrap">
      <div id="left">
        <el-table
          :key="tableKey"
          v-loading="listLoading"
          :data="list"
          @row-click="rowClick"
          border
          fit
          highlight-current-row
          style="width: 100%;"
        >
          <el-table-column
            label="序号"
            prop="id"
            align="center"
            width="80"
            :class-name="getSortClass('id')"
          >
            <template slot-scope="{row}">
              <span>{{ row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="设备编号"
            prop="deviceNo"
            align="center"
            width="120"
            :class-name="getSortClass('ammeterNo')"
          >
            <template slot-scope="{row}">
              <span>{{ row.deviceNo }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="设备名称"
            prop="deviceName"
            align="center"
            width="120"
            :class-name="getSortClass('ammeterName')"
          >
            <template slot-scope="{row}">
              <span>{{ row.deviceName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="设备类型"
            prop="deviceCategoryId"
            align="center"
            width="120"
          >
            <template slot-scope="{row}">
              <span>{{ row.deviceCategoryId }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="设备型号"
            prop="deviceType"
            align="center"
            width="120"
            :class-name="getSortClass('mRoom')"
          >
            <template slot-scope="{row}">
              <span>{{ row.deviceType }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
            <template slot-scope="{row,$index}">
              <el-button type="primary" size="mini" @click="handleUpdate(row)">
                编辑
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.limit"
          @pagination="getList"
        />
      </div>
      <div id="right">
        <div class="v-wrap">
          <!--          <div class="top">-->
          <!--            <el-form-->
          <!--              ref="dataForm"-->
          <!--              :rules="rules"-->
          <!--              :model="temp"-->
          <!--              class="inner-wrap"-->
          <!--              label-position="left"-->
          <!--              label-width="auto"-->
          <!--            >-->
          <!--              <el-dialog-->
          <!--                width="30%"-->
          <!--                :title="'增加'"-->
          <!--                :visible.sync="addCategoryShow"-->
          <!--                append-to-body-->
          <!--              >类型:-->
          <!--                <el-input class="inner-dialog" v-model="addCategoryName"/>-->
          <!--                <div slot="footer" class="dialog-footer">-->
          <!--                  <el-button @click="addCategoryShow = false">-->
          <!--                    取消-->
          <!--                  </el-button>-->
          <!--                  <el-button type="primary" @click="addCategory">-->
          <!--                    确认-->
          <!--                  </el-button>-->
          <!--                </div>-->
          <!--              </el-dialog>-->
          <!--              &lt;!&ndash;        分割线&ndash;&gt;-->
          <!--              <el-dialog-->
          <!--                width="30%"-->
          <!--                :title="'增加楼层'"-->
          <!--                :visible.sync="addFloorShow"-->
          <!--                append-to-body-->
          <!--              >楼层:-->
          <!--                <el-input class="inner-dialog" v-model="addFloorName"/>-->
          <!--                <div slot="footer" class="dialog-footer">-->
          <!--                  <el-button @click="addFloorShow = false">-->
          <!--                    取消-->
          <!--                  </el-button>-->
          <!--                  <el-button type="primary" @click="addFloor">-->
          <!--                    确认-->
          <!--                  </el-button>-->
          <!--                </div>-->
          <!--              </el-dialog>-->
          <!--              &lt;!&ndash;              更新的左边&ndash;&gt;-->
          <!--              <div class="inner-left">-->
          <!--                <el-form-item label="设备编号" class="update-input" prop="deviceNo">-->
          <!--                  <el-input v-model="temp.deviceNo"/>-->
          <!--                </el-form-item>-->
          <!--                <el-form-item label="设备名称" class="update-input" prop="deviceName">-->
          <!--                  <el-input v-model="temp.deviceName"/>-->
          <!--                </el-form-item>-->
          <!--                <el-form-item label="设备类型" prop="deviceCategoryId">-->
          <!--                  <el-select v-model="temp.deviceCategoryId" style="width: 150px" filterable placeholder="请选择">-->
          <!--                    <el-option-->
          <!--                      v-for="item in categoryList"-->
          <!--                      :key="item.id"-->
          <!--                      :label="item.name"-->
          <!--                      :value="item.id">-->
          <!--                    </el-option>-->
          <!--                  </el-select>-->
          <!--                  <el-button type="primary" @click="addCategoryShow = true">增加型号</el-button>-->
          <!--                </el-form-item>-->
          <!--                <el-form-item label="设备型号" class="update-input" prop="deviceType">-->
          <!--                  <el-input v-model="temp.deviceType"/>-->
          <!--                </el-form-item>-->
          <!--                <el-form-item label="楼层" prop="floorId">-->
          <!--                  <el-select v-model="temp.floorId" style="width: 150px" filterable placeholder="请选择">-->
          <!--                    <el-option-->
          <!--                      v-for="item in floorList"-->
          <!--                      :key="item.id"-->
          <!--                      :label="item.name"-->
          <!--                      :value="item.id">-->
          <!--                    </el-option>-->
          <!--                  </el-select>-->
          <!--                  <el-button type="primary" @click="addFloorShow = true">增加楼层</el-button>-->
          <!--                </el-form-item>-->
          <!--              </div>-->
          <!--              <div class="inner-right">-->
          <!--                <el-form-item label="电压" class="update-input-right" prop="voltage">-->
          <!--                  <el-input v-model="temp.voltage"/>-->
          <!--                </el-form-item>-->
          <!--                <el-form-item label="电流" class="update-input-right" prop="electricity">-->
          <!--                  <el-input v-model="temp.electricity"/>-->
          <!--                </el-form-item>-->
          <!--                <el-form-item label="功率" class="update-input-right" prop="power">-->
          <!--                  <el-input v-model="temp.power"/>-->
          <!--                </el-form-item>-->
          <!--                <el-form-item label="生产线" class="update-input-right" prop="productLine">-->
          <!--                  <el-input v-model="temp.productLine"/>-->
          <!--                </el-form-item>-->
          <!--                <el-form-item label="备注" class="update-input-right" prop="remark">-->
          <!--                  <el-input v-model="temp.remark"/>-->
          <!--                </el-form-item>-->
          <!--              </div>-->
          <!--              <div style="width: 100%; margin-bottom: 0px;display: flex;justify-content: center;">-->

          <!--                <el-button type="primary" @click="updateData">更新设备信息</el-button>-->
          <!--              </div>-->
          <!--            </el-form>-->
          <!--          </div>-->
          <div class="bottom">
            <el-table
              :key="tableKey"
              v-loading="listLoading"
              :data="accessoryList"
              @row-click="rowClick"
              border
              fit
              highlight-current-row
              style="width: 100%;"
              @sort-change="sortChange"
            >
              <el-table-column
                label="序号"
                prop="id"
                align="center"
                width="80"
                :class-name="getSortClass('id')"
              >
                <template slot-scope="{row}">
                  <span>{{ row.id }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="配件编号"
                prop="accessoryNo"
                align="center"
                width="80"
                :class-name="getSortClass('ammeterNo')"
              >
                <template slot-scope="{row}">
                  <span>{{ row.accessoryNo }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="配件名称"
                prop="accessoryName"
                align="center"
                width="80"
                :class-name="getSortClass('ammeterNo')"
              >
                <template slot-scope="{row}">
                  <span>{{ row.accessoryName }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="配件类型"
                prop="accessoryCategory"
                align="center"
                width="80"
                :class-name="getSortClass('ammeterName')"
              >
                <template slot-scope="{row}">
                  <span>{{ row.accessoryCategory }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="配件型号"
                prop="deviceCategoryId"
                align="center"
                width="120"
              >
                <template slot-scope="{row}">
                  <span>{{ row.accessoryType }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="生产厂家"
                prop="accessoryMaker"
                align="center"
                width="120"
                :class-name="getSortClass('mRoom')"
              >
                <template slot-scope="{row}">
                  <span>{{ row.accessoryMaker }}</span>
                </template>
              </el-table-column>
            </el-table>
            <el-button type="primary" @click="accessoryNotAddListGet">添加配件</el-button>
          </div>
        </div>
      </div>
    </div>
    <el-dialog title="添加配件" :visible.sync="accessoryDialogShow">
      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="accessoryNotAddList"
        @row-click="rowClick"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        @sort-change="sortChange"
        @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          label="序号"
          prop="id"
          sortable="custom"
          align="center"
          width="80"
          :class-name="getSortClass('id')"
        >
          <template slot-scope="{row}">
            <span>{{ row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="配件编号"
          prop="accessoryNo"
          sortable="custom"
          align="center"
          width="80"
          :class-name="getSortClass('ammeterNo')"
        >
          <template slot-scope="{row}">
            <span>{{ row.accessoryNo }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="配件名称"
          prop="accessoryName"
          sortable="custom"
          align="center"
          width="80"
          :class-name="getSortClass('ammeterNo')"
        >
          <template slot-scope="{row}">
            <span>{{ row.accessoryName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="配件类型"
          prop="accessoryCategory"
          sortable="custom"
          align="center"
          width="80"
          :class-name="getSortClass('ammeterName')"
        >
          <template slot-scope="{row}">
            <span>{{ row.accessoryCategory }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="配件型号"
          prop="deviceCategoryId"
          sortable="custom"
          align="center"
          width="120"
        >
          <template slot-scope="{row}">
            <span>{{ row.accessoryType }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="生产厂家"
          prop="accessoryMaker"
          sortable="custom"
          align="center"
          width="120"
          :class-name="getSortClass('mRoom')"
        >
          <template slot-scope="{row}">
            <span>{{ row.accessoryMaker }}</span>
          </template>
        </el-table-column>
      </el-table>
      <el-button type="primary" @click="addAccessory">添加配件</el-button>
    </el-dialog>


    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="auto"
        style="width: 400px; margin-left:50px;"
      >
        <el-dialog
          width="30%"
          :title="'增加'"
          :visible.sync="addCategoryShow"
          append-to-body
        >类型:
          <el-input class="inner-dialog" v-model="addCategoryName"/>
          <div slot="footer" class="dialog-footer">
            <el-button @click="addCategoryShow = false">
              取消
            </el-button>
            <el-button type="primary" @click="addCategory">
              确认
            </el-button>
          </div>
        </el-dialog>
        <!--        分割线-->
        <el-dialog
          width="30%"
          :title="'增加楼层'"
          :visible.sync="addFloorShow"
          append-to-body
        >楼层:
          <el-input class="inner-dialog" v-model="addFloorName"/>
          <div slot="footer" class="dialog-footer">
            <el-button @click="addFloorShow = false">
              取消
            </el-button>
            <el-button type="primary" @click="addFloor">
              确认
            </el-button>
          </div>
        </el-dialog>
        <el-form-item label="设备编号" prop="deviceNo">
          <el-input v-model="temp.deviceNo"/>
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="temp.deviceName"/>
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceCategoryId">
          <el-select v-model="temp.deviceCategoryId" filterable placeholder="请选择">
            <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
          <el-button type="primary" @click="addCategoryShow = true">增加型号</el-button>
        </el-form-item>
        <el-form-item label="设备型号" prop="deviceType">
          <el-input v-model="temp.deviceType"/>
        </el-form-item>
        <el-form-item label="电压" prop="voltage">
          <el-input v-model="temp.voltage"/>
        </el-form-item>
        <el-form-item label="电流" prop="electricity">
          <el-input v-model="temp.electricity"/>
        </el-form-item>
        <el-form-item label="功率" prop="power">
          <el-input v-model="temp.power"/>
        </el-form-item>
        <el-form-item label="生产线" prop="productLine">
          <el-input v-model="temp.productLine"/>
        </el-form-item>
        <el-form-item label="楼层" prop="floorId">
          <el-select v-model="temp.floorId" filterable placeholder="请选择">
            <el-option
              v-for="item in floorList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
          <el-button type="primary" @click="addFloorShow = true">增加楼层</el-button>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="temp.remark"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogPvVisible" title="Reading statistics">
      <el-table :data="pvData" border fit highlight-current-row style="width: 100%">
        <el-table-column prop="key" label="Channel"/>
        <el-table-column prop="pv" label="Pv"/>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogPvVisible = false">Confirm</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<style scoped>
  .v-wrap {
    display: flex;
    height: 100%;
    flex-direction: column;
  }

  .inner-dialog {
    width: 300px;
  }

  .top {
    width: 100%;
    flex: 1;
  }

  .bottom {
    width: 100%;
    border-top: 1px solid #E6E6E6;
    flex: 1;
  }

  .update-input {
    width: 300px;
  }

  .update-input-right {
    width: 300px;
    height: 36px;
  }

  .wrap {
    margin: 0 auto;
    height: 800px;
    width: 100%;
    border: 1px solid #E6E6E6;
  }

  #left {
    width: 50%;
    border-right: 1px solid #E6E6E6;
    float: left;
  }

  .inner-wrap {
    margin: 0 auto;
    height: 100%;
    width: 100%;
    border: 1px solid #E6E6E6;
  }

  .inner-left {
    width: 50%;
    padding: 50px 0px 0px 50px;
    border-right: 1px solid #E6E6E6;
    float: left;
  }

  .inner-right {
    width: 50%;
    padding: 50px 0px 0px 50px;
    margin-left: 50%;
  }

  #right {
    width: 50%;
    height: 100%;
    margin-left: 50%;
  }
</style>
<script>
  import { fetchList, fetchPv, createArticle, updateArticle } from '@/api/article'
  import waves from '@/directive/waves' // waves directive
  import { parseTime } from '@/utils'
  import { service2 as http } from '@/utils/request'
  import Pagination from '@/components/Pagination' // secondary package based on el-pagination

  const calendarTypeOptions = [
    { key: 'CN', display_name: 'China' },
    { key: 'US', display_name: 'USA' },
    { key: 'JP', display_name: 'Japan' },
    { key: 'EU', display_name: 'Eurozone' }
  ]

  // arr to obj, such as { CN : "China", US : "USA" }
  const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
    acc[cur.key] = cur.display_name
    return acc
  }, {})

  export default {
    name: 'ComplexTable',
    components: { Pagination },
    directives: { waves },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'info',
          deleted: 'danger'
        }
        return statusMap[status]
      },
      typeFilter(type) {
        return calendarTypeKeyValue[type]
      },
      role(type) {
        const name = [0,
          '普通人员',
          '管理员',
          '集团人员']
        return name[type]
      }
    },
    data() {
      const validateNormal = (rule, value, callback) => {
        if (value.length < 0) {
          callback(new Error('不能为空'))
        } else {
          callback()
        }
      }
      const notNegative = (rule, value, callback) => {
        if (value < 0) {
          callback(new Error('不能为空'))
        } else {
          callback()
        }
      }
      return {
        tableKey: 0,
        list: null,
        total: 0,
        listLoading: true,
        addCategoryShow: false,
        accessoryDialogShow: false,
        addCategoryName: '',
        accessoryList: [],
        accessoryNotAddList: [],
        categoryList: [],
        floorList: [],
        commitAccessoryIdArr: '',
        addFloorName: '',
        addFloorShow: false,
        commitRules: {
          name: [{ required: true, trigger: 'blur', validator: validateNormal }],
          password: [{ required: true, trigger: 'blur', validator: validateNormal }]
        },
        listQuery: {
          page: 1,
          limit: 20,
          importance: undefined,
          title: undefined,
          type: undefined,
          sort: '+id'
        },
        importanceOptions: [1, 2, 3],
        calendarTypeOptions,
        sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
        statusOptions: ['published', 'draft', 'deleted'],
        showReviewer: false,
        temp: {
          id: undefined,
          deviceNo: '',
          deviceName: '',
          deviceCategoryId: -1,
          deviceType: '',
          voltage: 0,
          electricity: 0,
          power: 0,
          productLine: '',
          floorId: -1,
          remark: ''
        },
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: '编辑',
          create: '添加'
        },
        dialogPvVisible: false,
        pvData: [],
        rules: {
          deviceNo: [{ required: true, message: '需要填写', trigger: 'blur' }],
          // timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
          deviceName: [{ required: true, message: '需要填写', trigger: 'blur' }],
          deviceCategoryId: [{ required: true, message: '需要填写', trigger: 'change', validator: notNegative }],
          floorId: [{ required: true, message: '需要填写', trigger: 'change', validator: notNegative }],
          deviceType: [{ required: true, message: '需要填写', trigger: 'blur' }]
        },
        downloadLoading: false

      }
    },
    created() {
      this.getList()
    },
    methods: {
      addAccessory() {
        http({
          url: '/accessory/saveRelationship',
          method: 'post',
          params: {
            deviceId: this.temp.id,
            accessoryIdArr: this.commitAccessoryIdArr
          }
        }).then(res => {
          if (1 != res.code) {
            this.$notify({
              title: 'error',
              message: '请联系管理员',
              type: 'error',
              duration: 2000
            })
            return
          }
          this.getRelationshipAccessory()
          this.accessoryDialogShow = false
        })
      },
      handleSelectionChange(rows) {
        let idArr = ''
        rows.map((item) => {
          idArr = idArr + item.id + ','
        })
        this.commitAccessoryIdArr = idArr
      },
      accessoryNotAddListGet() {
        if (!this.temp.id) {
          this.$notify({
            title: 'error',
            message: '请选择一条记录',
            type: 'warning',
            duration: 2000
          })
          return
        }
        http({
          url: '/accessory/accessoryNotAddList',
          method: 'get',
          params: {
            id: this.temp.id
          }
        }).then(res => {
          if (0 == res.list.length) {
            this.$notify({
              title: 'error',
              message: '全部配件已经添加',
              type: 'warning',
              duration: 2000
            })
            return
          }
          this.accessoryNotAddList = res.list
          this.accessoryDialogShow = true
        })
      },
      addCategory() {
        http({
          url: '/device/saveCategory',
          method: 'post',
          params: {
            name: this.addCategoryName
          }
        }).then(res => {
          this.addCategoryShow = false
          this.getCategory()
        })
      },
      addFloor() {
        http({
          url: '/device/saveFloor',
          method: 'post',
          params: {
            name: this.addFloorName
          }
        }).then(res => {
          this.addFloorShow = false
          this.getFloor()
        })
      },
      rowClick(row) {
        this.temp = Object.assign({}, row)
        this.getRelationshipAccessory()
      },
      getRelationshipAccessory() {
        http({
          url: '/accessory/relationship',
          method: 'get',
          params: {
            id: this.temp.id
          }
        }).then(res => {
          this.accessoryList = res.list
        })
      },
      getList() {
        this.listLoading = true
        http({
          url: '/device/getDeviceList',
          method: 'get',
          params:{
            ...this.listQuery
          }
        }).then(res => {
          // console.info('res', res)
          this.list = res.list
          this.total = res.count
          this.listLoading = false
          this.getCategory()
          this.getFloor()
        })
      },
      getCategory() {
        http({ url: '/device/categoryList', method: 'get' }).then(res => {
          this.categoryList = res.list
          this.categoryList.push({ id: -1, name: '请选择' })
        })
      },
      getFloor() {
        http({ url: '/device/floorList', method: 'get' }).then(res => {
          this.floorList = res.list
          this.floorList.push({ id: -1, name: '请选择' })
        })
      },
      handleModifyStatus(row, status) {
        this.$message({
          message: '操作Success',
          type: 'success'
        })
        row.status = status
      },
      sortChange(data) {
        const { prop, order } = data
        if (prop === 'id') {
          this.sortByID(order)
        }
      },
      sortByID(order) {
        if (order === 'ascending') {
          this.listQuery.sort = '+id'
        } else {
          this.listQuery.sort = '-id'
        }
        this.handleFilter()
      },
      resetTemp() {
        this.temp = {
          id: undefined,
          deviceNo: '',
          deviceName: '',
          deviceCategoryId: -1,
          deviceType: '',
          voltage: 0,
          electricity: 0,
          power: 0,
          productLine: '',
          floorId: -1,
          remark: ''
        }
      },
      handleCreate() {
        this.resetTemp()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            http({
              url: '/device/saveDevice',
              method: 'post',
              params: { ...this.temp }
            }).then(res => {
              if (res.code != 1) {
                this.$notify({
                  title: 'error',
                  message: '联系管理员',
                  type: 'warning',
                  duration: 2000
                })
                return
              }
              this.dialogFormVisible = false
              this.$notify({
                title: 'ok',
                message: '添加成功',
                type: 'success',
                duration: 2000
              })
              this.resetTemp()
              this.getList()
            })
          }
        })
      },
      handleUpdate(row) {
        this.ammeterNoDis = true
        this.temp = Object.assign({}, row) // copy obj
        // this.temp.timestamp = new Date(this.temp.timestamp)
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.getRelationshipAccessory()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (!this.temp.id) {
            this.$notify({
              title: 'error',
              message: '请选择具体的项目进行更新',
              type: 'warning',
              duration: 2000
            })
            return
          }
          if (valid) {
            http({
              url: '/device/updateDevice',
              method: 'post',
              params: { ...this.temp }
            }).then(res => {
              if (res.code != 1) {
                this.$notify({
                  title: '警告',
                  message: '重复的姓名',
                  type: 'error',
                  duration: 2000
                })
                return
              }
              this.dialogFormVisible = false
              this.$notify({
                title: 'ok',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
              this.getList()
            })
          }
        })
      },
      handleDelete(row, index) {
        this.$notify({
          title: 'Success',
          message: 'Delete Successfully',
          type: 'success',
          duration: 2000
        })
        this.list.splice(index, 1)
      },
      handleFetchPv(pv) {
        fetchPv(pv).then(response => {
          this.pvData = response.data.pvData
          this.dialogPvVisible = true
        })
      },
      handleDownload() {
        this.downloadLoading = true
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['timestamp', 'title', 'type', 'importance', 'status']
          const filterVal = ['timestamp', 'title', 'type', 'importance', 'status']
          const data = this.formatJson(filterVal)
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: 'table-list'
          })
          this.downloadLoading = false
        })
      },
      formatJson(filterVal) {
        return this.list.map(v => filterVal.map(j => {
          if (j === 'timestamp') {
            return parseTime(v[j])
          } else {
            return v[j]
          }
        }))
      },
      getSortClass: function(key) {
        const sort = this.listQuery.sort
        return sort === `+${key}` ? 'ascending' : 'descending'
      }
    }
  }
</script>
